package socket;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * 聊天室的客户端
 */
public class Client {
    private Socket socket;
    public Client(){
        try {
            System.out.println("客户端连接服务器中");
            socket = new Socket("localhost",8088);  //176.148.5.91  localhost
            System.out.println("客户端连接服务器成功");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start(){
        System.out.println("开始聊天吧!!!");
        Scanner sc = new Scanner(System.in);
        try {
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
            BufferedWriter bw = new BufferedWriter(osw);
            PrintWriter pw = new PrintWriter(bw,true);
            while (true){
                System.out.println("Crisp");
                String in = sc.next();
                if ("exit".equals(in)){
                    break;
                }
                pw.println(in);

            }

        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close(); //与服务端断开连接
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        Client client = new Client();
        client.start();

    }
}
